xquery version "1.0";

(:~
	Handles all the modifications in the Database. It includes
	New Records, Editing Records and Deleting Records. All functions
	are required to have an access to the database.
:)

declare namespace MedEx="http://localhost:8080/exist/MedEx";
declare namespace system="http://exist-db.org/xquery/system";
import module namespace util="http://exist-db.org/xquery/util";
import module namespace request="http://exist-db.org/xquery/request";
import module namespace session="http://exist-db.org/xquery/session";
import module namespace xdb="http://exist-db.org/xquery/xmldb";
 
declare option exist:serialize "method=xhtml media-type=text/xml indent=yes";

declare function MedEx:get-sections() as text()*{	
	let $sequence := collection(session:get-attribute('collection'))//Format/Section/name/text()
	return
		$sequence
};

(: Deletes a Record using the ID :)
declare function MedEx:delete-record($id as xs:int*) as element()*{
    for $rec in collection(session:get-attribute('collection'))//RECORD[id = $id]
    return 
        update value $rec with <NULL/>
};

(: Deletes the Record then adds the updated record :)
declare function MedEx:edit-record($id as xs:int?, $xml as xs:string*) as element()*{
	let $null := MedEx:delete-record($id)
    let $filename := fn:concat($id, ".xml")
	let $collection := fn:concat(fn:concat('xmldb:exist://',session:get-attribute('collection')),'/record')
	let $login := xmldb:login($collection, 'admin', 'admin')
	let $store-status := xmldb:store($collection, $filename, $xml, "text/xml")
	return <success>Record Successfully updated in the database</success>
};

(: Saves the new record to a new file :)
declare function MedEx:save-record($xml as xs:string*) as element()?{
	let $filename := fn:concat(max(collection(session:get-attribute('collection'))//RECORD/id/text())+1, ".xml")
	let $collection := fn:concat(fn:concat('xmldb:exist://',session:get-attribute('collection')),'/record')
	let $login := xmldb:login($collection, 'admin', 'admin')
	let $store-status := xmldb:store($collection, $filename, $xml, "text/xml")
	return <success>Record Successfully stored in the database</success>
    
};

(: Gets the Record using an ID. For Editing :)
declare function MedEx:get-record($id as xs:int?) as element()*{
	collection(session:get-attribute('collection'))//RECORD[id = $id]
};

(: Gets the Highest Number of ID + 1 for new Records :)
declare function MedEx:get-next-ID() as element()?{
	<ID>{max(collection(session:get-attribute('collection'))//RECORD/id/text())+1}</ID>
};

session:create(),
let $command := request:get-parameter("command", ())
let $id := xs:int(request:get-parameter("id", ()))
let $xml := request:get-parameter("xml", ())
return
    if ($command eq 'new') then
        MedEx:save-record($xml)
    else if($command eq 'edit') then
        MedEx:edit-record($id, $xml)
	else if($command eq 'delete') then
		MedEx:delete-record($id)
	else if($command eq 'id') then
		MedEx:get-next-ID()
	else
		MedEx:get-record($id)
